package com.szyjjk.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szyjjk.entity.*;
import com.szyjjk.mapper.*;
import com.szyjjk.util.DateUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author yangyue
 * @Description //TODO 主要是针对深圳气象局的数据  外加爬虫
 * @Date 10:16 2020/3/24
 * @Param
 * @return
 **/

@Service
@Transactional(readOnly = true)
public class RunSiteService {
    @Autowired
    private RunSiteMapper runSiteMapper;

    @Autowired
    private RunSiteDataMapper runSiteDataMapper;

    @Autowired
    private RunAreaMapper runAreaMapper;

    @Autowired
    private RunSiteHKObsMapper runSiteHKObsMapper;


    @Autowired
    private SysWebCrawlerMapper    sysWebCrawlerMapper;





    /**
     * @Author yangyue
     * @Description //TODO 根据itle查询crawler
     * @Date 15:17 2020/4/22
     * @Param [title]
     * @return com.szyjjk.entity.SysWebCrawler
     **/
    public SysWebCrawler getSycCralwerByTitle(String title){
        SysWebCrawlerExample example = new SysWebCrawlerExample();
         example.createCriteria().andCrawlerTitleEqualTo(title);
         List<SysWebCrawler> allCralwer=sysWebCrawlerMapper.selectByExample(example);
         if(allCralwer.size()>0){
             return allCralwer.get(0);
         }else{
             return  null;
         }
    }

    /**
     * @Author yangyue
     * @Description //TODO 修改爬虫
     * @Date 15:18 2020/4/22
     * @Param [SysWebCrawler]
     * @return java.lang.Boolean
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean editSysWebCrawler(SysWebCrawler sysWebCrawler) {
        try {
            //todo 可填加后续逻辑
            sysWebCrawlerMapper.updateByPrimaryKeySelective(sysWebCrawler);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * @Author yangyue
     * @Description //TODO 爬虫管理查询全部
     * @Date 14:33 2020/4/22
     * @Param []
     * @return java.util.List<com.szyjjk.entity.SysWebCrawler>
     **/
    public List<SysWebCrawler> findSysWebCrawlerAll() {
        return sysWebCrawlerMapper.selectByExample(new SysWebCrawlerExample());
    }
    /**
     * @Author yangyue
     * @Description //TODO 爬虫分页查询
     * @Date 10:30 2020/4/22
     * @Param [pageNo, pageSize]
     * @return java.util.List<com.szyjjk.entity.RunSiteData>
     **/
 
    public List<SysWebCrawler> findSysWebCrawlerPage(Integer pageNo, Integer pageSize) {
        SysWebCrawlerExample example = new SysWebCrawlerExample();
        example.setOrderByClause("crawler_time desc limit " + pageNo + " , " + pageSize);
        return sysWebCrawlerMapper.selectByExample(example);
    }

    /**
     * @Author yangyue
     * @Description //TODO 返回爬虫总数
     * @Date 10:38 2020/4/22
     * @Param []
     * @return int
     **/
    public  int  findSysWebCrawlerPagecCount() {
        SysWebCrawlerExample example = new SysWebCrawlerExample();
        return sysWebCrawlerMapper.selectByExample(example).size();
    }

    /**
     * @Author yangyue
     * @Description //TODO 根据ID 查询
     * @Date 10:00 2020/4/23
     * @Param [id]
     * @return com.szyjjk.entity.SysWebCrawler
     **/
    public SysWebCrawler getbyiDSysCralwer(String id){
        return sysWebCrawlerMapper.selectByPrimaryKey(id);
    }


    /*
     * @Author yangyue
     * @Description //TODO 返回所有区域
     * @Date 11:19 2020/3/26
     * @Param []
     * @return java.util.List<com.szyjjk.entity.RunArea>
     **/
    public List<RunArea> findAllArea() {
        return this.runAreaMapper.selectByExample(null);
    }


    /**
     * @return java.util.List<com.szyjjk.entity.RunSite>
     * @Author yangyue
     * @Description //TODO 根据区域查询站点
     * @Date 11:20 2020/3/26
     * @Param [areaID]
     **/
    public List<RunSite> findSiteByAreaID(String areaID) {
        RunSiteExample example = new RunSiteExample();
        example.createCriteria().andAreaIdEqualTo(areaID);
        return runSiteMapper.selectByExample(example);
    }


    /**
     * @return java.util.List<com.szyjjk.entity.RunSiteData>
     * @Author yangyue
     * @Description //TODO 根据ID 和列表排序查询
     * @Date 16:24 2020/3/27
     * @Param [siteID, siteType]
     **/
    public List<RunSiteData> findSiteDataMap(String siteID, String siteType) {
        RunSiteDataExample example = new RunSiteDataExample();
        example.createCriteria().andSiteIdEqualTo(siteID).andDataTypeEqualTo(siteType);
        example.setOrderByClause("data_time desc limit 1");
        return runSiteDataMapper.selectByExample(example);
    }


    /**
     * @return java.util.List<com.szyjjk.entity.RunSite>
     * @Author yangyue
     * @Description //TODO 查询站点  根据类别
     * @Date 10:22 2020/3/24
     * @Param [name]
     **/
    public List<RunSite> findSiteByType(String name) {
        RunSiteExample example = new RunSiteExample();
        example.createCriteria().andSiteTypeEqualTo(name);
        return runSiteMapper.selectByExample(example);
    }


    /**
     * @return com.szyjjk.entity.RunSite
     * @Author yangyue
     * @Description //TODO
     * @Date 16:30 2020/3/27
     * @Param [id]
     **/
    public RunSite findbyIdSite(String id) {
        return runSiteMapper.selectByPrimaryKey(id);
    }


    /**
     * @return java.util.List<com.szyjjk.entity.RunSiteData>
     * @Author yangyue
     * @Description //TODO 根据站点ID和时间查询站点数据
     * @Date 10:33 2020/3/24
     * @Param [Runsiteid, time]
     **/
    public List<RunSiteData> findSiteDataByTime(String Runsiteid, Date time) {
        RunSiteDataExample example = new RunSiteDataExample();
        example.createCriteria().andDataTimeEqualTo(time).andSiteIdEqualTo(Runsiteid);
        return runSiteDataMapper.selectByExample(example);
    }

    /**
     * @return java.util.List<com.szyjjk.entity.RunSiteData>
     * @Author yangyue
     * @Description //TODO 根据站点ID和时间加上类别查询站点数据
     * @Date 15:15 2020/4/8
     * @Param [Runsiteid, time, type]
     **/
    public List<RunSiteData> findSiteDataByTimeAndType(String Runsiteid, Date time, String type) {
        RunSiteDataExample example = new RunSiteDataExample();
        example.createCriteria().andDataTimeEqualTo(time).andSiteIdEqualTo(Runsiteid).andDataTypeEqualTo(type);
//        example.setOrderByClause();
        return runSiteDataMapper.selectByExample(example);
    }

    /**
     * @return java.util.List<com.szyjjk.entity.RunSiteData>
     * @Author yangyue
     * @Description //TODO 分页查询深圳气象局数据
     * @Date 15:36 2020/4/14
     * @Param [pageNo, pageSize, siteID, begTime, endTime]
     **/
    public List<RunSiteData> findsitrDatePage(Integer pageNo, Integer pageSize, String siteID, Date begTime, Date endTime) {
        RunSiteDataExample example = new RunSiteDataExample();
        if (begTime == null) {
            example.createCriteria().andSiteIdEqualTo(siteID);
        } else {
            example.createCriteria().andSiteIdEqualTo(siteID).andDataTimeBetween(begTime, endTime);
        }
        example.setOrderByClause("data_time desc limit " + pageNo + " , " + pageSize);
        return runSiteDataMapper.selectByExample(example);
    }

    public int findsitrDatePageCount(String siteID, Date begTime, Date endTime) {
        RunSiteDataExample example = new RunSiteDataExample();
        if (begTime == null) {
            example.createCriteria().andSiteIdEqualTo(siteID);
        } else {
            example.createCriteria().andSiteIdEqualTo(siteID).andDataTimeBetween(begTime, endTime);
        }
        return runSiteDataMapper.selectByExample(example).size();
    }


    /**
     * @return java.lang.Boolean
     * @Author yangyue
     * @Description //TODO 保存站点数据
     * @Date 10:26 2020/3/24
     * @Param [runSite]
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean saveRunSiteDate(RunSiteData runSiteDate) {
        try {
            //todo 可填加后续逻辑
            runSiteDataMapper.insertSelective(runSiteDate);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * @return java.lang.Boolean
     * @Author yangyue 修改深圳数据
     * @Description //TODO
     * @Date 10:07 2020/4/21
     * @Param [runSiteDate]
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean editRunSiteDate(RunSiteData runSiteDate) {
        try {
            //todo 可填加后续逻辑
            runSiteDataMapper.updateByPrimaryKeySelective(runSiteDate);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    // 通过名称和类别查询Area
    public List<RunArea> findAreaByTypeName(String Type, String Name) {
        RunAreaExample example = new RunAreaExample();
        example.createCriteria().andAreaNameEqualTo(Name).andAreaTypeEqualTo(Type);
        return runAreaMapper.selectByExample(example);
    }

    //保存Area
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean saveRunArea(RunArea runArea) {
        try {
            //todo 可填加后续逻辑
            runAreaMapper.insertSelective(runArea);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //保存site
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean saveRunSite(RunSite runsite) {
        try {
            //todo 可填加后续逻辑
            runSiteMapper.insertSelective(runsite);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //保存香港天文台
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean saveRunSiteHongKongObs(RunSiteHKObs runSiteHKObs) {
        try {
            //todo 可填加后续逻辑
            runSiteHKObsMapper.insertSelective(runSiteHKObs);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * @return java.util.List<com.szyjjk.entity.RunSiteHKObs>
     * @Author yangyue
     * @Description //TODO
     * @Date 16:19 2020/4/1
     * @Param [siteID, time]
     **/
    public int findSiteHongkongByDateAndSiteID(String siteID, Date time) {
        RunSiteHKObsExample example = new RunSiteHKObsExample();
        example.createCriteria().andObsTimeEqualTo(time).andSiteIdEqualTo(siteID);
        return runSiteHKObsMapper.countByExample(example);
    }


    /**
     * @return com.github.pagehelper.PageInfo<com.szyjjk.entity.RunSiteHKObs>
     * @Author yangyue
     * @Description //TODO 分页查询相关数据 第几页 一共几个  利用orderby 进行分页了
     * @Date 17:23 2020/4/13
     * @Param [pageNo, pageSize, siteID]
     **/
    public List<RunSiteHKObs> findPageSiteHKObs(Integer pageNo, Integer pageSize, String siteID, Date begTime, Date endTime) {
        PageHelper.startPage(pageNo, pageSize);
        RunSiteHKObsExample example = new RunSiteHKObsExample();
        if (StringUtils.isNotEmpty(siteID)) {
            if (begTime == null) {
                example.createCriteria().andSiteIdEqualTo(siteID);
            } else {
                example.createCriteria().andSiteIdEqualTo(siteID).andObsTimeBetween(begTime, endTime);
            }
            example.setOrderByClause("obs_time desc limit " + pageNo + " , " + pageSize);
        }
        return runSiteHKObsMapper.selectByExample(example);
    }


    /**
     * @return java.util.List<com.szyjjk.entity.RunSiteHKObs>
     * @Author yangyue
     * @Description //TODO 分页查询返回数量
     * @Date 14:10 2020/4/14
     * @Param [siteID, begTime, endTime]
     **/
    public int findPageSiteHKObsbyCount(String siteID, Date begTime, Date endTime) {
        RunSiteHKObsExample example = new RunSiteHKObsExample();
        if (StringUtils.isNotEmpty(siteID)) {
            if (begTime == null) {
                example.createCriteria().andSiteIdEqualTo(siteID);
            } else {
                example.createCriteria().andSiteIdEqualTo(siteID).andObsTimeBetween(begTime, endTime);
            }
        }
        return runSiteHKObsMapper.selectByExample(example).size();
    }


    public List<RunSiteHKObs> findsiteHongKongBySiteID(String siteID) {
        RunSiteHKObsExample example = new RunSiteHKObsExample();
        example.createCriteria().andSiteIdEqualTo(siteID);
        example.setOrderByClause("obs_time desc limit 1");
        return runSiteHKObsMapper.selectByExample(example);
    }

    /**
     *	 查询
     */
    public List<RunSiteData> findSiteDataOrderBy(String siteID, String siteType,Date date) {
        RunSiteDataExample example = new RunSiteDataExample();
        if(null==date) {
        	//查询全部时间
            example.createCriteria().andSiteIdEqualTo(siteID).andDataTypeEqualTo(siteType).andDataTimeLessThan(DateUtil.getDateMin());
        }else {
            example.createCriteria().andSiteIdEqualTo(siteID).andDataTypeEqualTo(siteType).andDataTimeGreaterThan(date).andDataTimeLessThan(DateUtil.getDateMin());
        }
        example.setOrderByClause("data_time");
        try {
        	 return runSiteDataMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}
       return null;
    }

    /**
     * @Author yangyue
     * @Description //TODO 查询只用type和data
     * @Date 10:43 2020/7/14
     * @Param [siteType, date]
     * @return java.util.List<com.szyjjk.entity.RunSiteData>
     **/
    public List<RunSiteData> findSiteDataChart( String siteID, String siteType,Date date) {
        RunSiteDataExample example = new RunSiteDataExample();
        example.createCriteria().andSiteIdEqualTo(siteID).andDataTypeEqualTo(siteType).andDataTimeEqualTo(date);
        example.setOrderByClause("data_time");
        return runSiteDataMapper.selectByExample(example);
    }

    /**
     * 查询数据根据时间
     * @param siteID
     * @param siteType
     * @param
     * @return
     */
    public List<RunSiteData> findSiteDataOrderByDate(String siteID, String siteType,RunModelInfo run) {
    	RunSiteDataExample example = new RunSiteDataExample();
    	
    	example.createCriteria().andSiteIdEqualTo(siteID).andDataTypeEqualTo(siteType)
    	.andDataTimeGreaterThanOrEqualTo(run.getStartDate())
    	.andDataTimeLessThanOrEqualTo(run.getEndDate());
    	
    	example.setOrderByClause("data_time");
    	try {
    		return runSiteDataMapper.selectByExample(example);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return null;
    }
    
    /**
     *	 查询
     */
    public List<RunSiteHKObs> findHkobs(String siteID) {
    	 RunSiteHKObsExample example = new RunSiteHKObsExample();
         example.createCriteria().andSiteIdEqualTo(siteID);
         example.setOrderByClause("obs_time");
        try {
        	 return runSiteHKObsMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}
       return null;
    }
    /**
     * 指定范围查询
     * @param siteId
     * @param run
     * @return
     */
	public List<RunSiteHKObs> findHkobsByDate(String siteId, RunModelInfo run) {
		RunSiteHKObsExample example = new RunSiteHKObsExample();
        example.createCriteria().andSiteIdEqualTo(siteId).
        andObsTimeGreaterThanOrEqualTo(run.getStartDate()).andObsTimeLessThanOrEqualTo( run.getEndDate());
        example.setOrderByClause("obs_time");
       try {
       	 return runSiteHKObsMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}
      return null;
	}

	public List<RunSiteData>  findWeekly(String zhand,String mondayDate, String sundayDate) {
		RunSiteDataExample example = new RunSiteDataExample();
    	example.createCriteria().andSiteIdEqualTo(zhand)
    	.andDataTimeGreaterThanOrEqualTo(DateUtil.stringToDate2(mondayDate))
    	.andDataTimeLessThanOrEqualTo(DateUtil.stringToDate2(sundayDate));
    	
    
    	try {
    		return runSiteDataMapper.selectByExample(example);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return new ArrayList<>();
		
	}

    public List<RunSite> findAllSite() {
        RunSiteExample ex = new RunSiteExample();
        return runSiteMapper.selectByExample(ex);
    }
}
